//
//  RootVC.swift
//  Z048
//
//  Created by remy on 2020/1/30.
//  Copyright © 2020 remy. All rights reserved.
//

class RootVC: UIViewController {
    private lazy var stage: Stage = {
        let stage = Stage(size: UIScreen.size)
        stage.scaleMode = .aspectFill
        stage.backgroundColor = FlatColor.wetAsphalt
        return stage
    }()
    override var prefersStatusBarHidden: Bool {
        return true
    }
    
    override func loadView() {
        // MARK: 不要调用super.loadView()
        self.view = SKView()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        guard let view = self.view as? SKView else { return }
        view.ignoresSiblingOrder = true
        #if DEBUG
            view.showsDrawCount = true
            view.showsNodeCount = true
            view.showsFPS = true
        #endif
        view.presentScene(stage)
        // 重置
//        let reset = UILabel(frame: CGRect(x: 16, y: Size.screenHeight - (Device.isAllScreen ? 94 : 60), width: Size.screenWidth - 32, height: 44), text: "reset".localized, textColor: SKColor.white, fontSize: 20, alignment: .center)
//        reset.backgroundColor = FlatColor.midnightBlue
//        reset.layer.cornerRadius = 8
//        reset.layer.masksToBounds = true
//        reset.isUserInteractionEnabled = true
//        reset.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(resetAction(_:))))
//        view.addSubview(reset)
        // 手势
//        let direction: Array<UISwipeGestureRecognizer.Direction> = [.left, .right, .up, .down]
//        for d in direction {
//            let swipe = UISwipeGestureRecognizer(target: self, action: #selector(swipeAction(_:)))
//            swipe.direction = d
//            view.addGestureRecognizer(swipe)
//        }
        // 通知
        _ = NotificationCenter.default.rx.notification(Notification.Name.GAME_QUIT)
            .takeUntil(self.rx.deallocated)
            .subscribe(onNext: { [weak self] _ in
                self?.stage.gameSave()
            })
    }
    
//    @objc func resetAction(_ sender: UITapGestureRecognizer) {
//        if !stage.isOver && stage.score > 0 {
//            let alert = UIAlertController(title: "restart".localized, message: nil, preferredStyle: .alert)
//            alert.addAction(UIAlertAction(title: "cancel".localized, style: .destructive))
//            alert.addAction(UIAlertAction(title: "confirm".localized, style: .default, handler: { (action: UIAlertAction) in
//                self.stage.gameReset()
//            }))
//            self.present(alert, animated: false, completion: nil)
//        } else {
//            stage.gameReset()
//        }
//    }
    
//    @objc func swipeAction(_ sender: UISwipeGestureRecognizer) {
//        stage.stageSwipe(direction: sender.direction)
//    }
}
